
package com.hd.sup.modules.alarm.controller;


import com.hd.common.constant.BizConstant;
import com.hd.common.constant.SysConstant;
import com.hd.common.page.BaseController;
import com.hd.common.reqres.request.RequestData;
import com.hd.common.reqres.response.Result;
import com.hd.common.utils.GenUtil;
import com.hd.sup.modules.alarm.model.AlarmDetail;
import com.hd.sup.modules.alarm.model.AlarmRecord;
import com.hd.sup.modules.alarm.service.IAlarmReordService;
import com.hd.sup.modules.alarm.wrapper.AlarmWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title AlarmRecordController.java
 * @Description 报警台账控制器
 * @Package com.hd.sup.modules.alarm.controller
 * @date 2021/1/2 7:37 下午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@RestController
@RequestMapping("/alarmRecord")
public class AlarmRecordController extends BaseController {

    @Resource
    private IAlarmReordService reordService;

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 处理报警
     * @date 2021/1/2 7:08 下午
     * @params [requestData]
     */
    @PostMapping("/handle")
    public Result<?> handle(AlarmDetail alarmDetail) {
        String alarmId = alarmDetail.getId();
        String handleDesc = alarmDetail.getHandleDesc();
        if (StringUtils.isBlank(handleDesc)) return Result.error("参数 handleDesc 为空");
        if (StringUtils.isBlank(alarmId)) return Result.error("参数 id 为空");

        //插入处理台账
        AlarmRecord alarmRecord = new AlarmRecord(GenUtil.newId(), BizConstant.AlarmRecordType.HANDLE_RECORD.key(), alarmDetail.getFileUrl(), alarmId, handleDesc, getCurrentUser().getUsername());
        alarmRecord.insert();

        //更新报警
        alarmDetail.setHandleStatus(BizConstant.HandleStatus.HANDLED.key());
        alarmDetail.updateById();

        return Result.success(SysConstant.SC_OK, "处理成功", null);
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 审核报警
     * @date 2021/1/2 7:08 下午
     * @params [requestData]
     */
    @PostMapping("/verify")
    public Result<?> verify(AlarmDetail alarmDetail) {
        String alarmId = alarmDetail.getId();
        String verifyDesc = alarmDetail.getVerifyDesc();
        if (StringUtils.isBlank(verifyDesc)) return Result.error("参数 verifyDesc 为空");
        if (StringUtils.isBlank(alarmId)) return Result.error("参数 id 为空");

        //插入审核台账
        AlarmRecord alarmRecord = new AlarmRecord(GenUtil.newId(), BizConstant.AlarmRecordType.VERIFY_RECORD.key(), alarmDetail.getFileUrl(), alarmId, verifyDesc, getCurrentUser().getUsername());
        alarmRecord.insert();

        //更新报警
        alarmDetail.setVerifyStatus(BizConstant.VerifyStatus.VERIFIED.key());
        alarmDetail.updateById();


        return Result.success(SysConstant.SC_OK, "审核成功", null);
    }

    /**
     * @return com.hd.sup.core.reqres.response.Result<?>
     * @author chentl
     * @description 查询 报警处理历史记录 or 审核历史记录
     * @date 2021/1/2 7:17 下午
     * @params [requestData]
     */
    @PostMapping("/list")
    public Result<?> recordList(@RequestBody RequestData requestData) {
        String recordType = requestData.getString("recordType");
        String alarmId = requestData.getString("alarmId");
//        if (StringUtils.isBlank(recordType)) return Result.error("参数 recordType 为空");
        if (StringUtils.isBlank(alarmId)) return Result.error("参数 alarmId 为空");

        AlarmRecord param = new AlarmRecord(recordType, alarmId);

        List<Map<String, Object>> result = (List<Map<String, Object>>) new AlarmWrapper(reordService.getList(param)).warp();
        return Result.success(result);
    }

}
